home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 549 / text0000.txt < prev   
Encoding:
Text File  |  1996-08-06  |  1.1 KB  |  32 lines

  1. alindbac@sw.seisy.abb.se (Anders Lindback) writes:
  2.  
  3. >> Why is it not allowed to delegate the initialisation to another
  4. >> constructor
  5. >> 
  6. >> class X
  7. >> {
  8. >> public:
  9. >> X (int i) : i_(i) {}
  10. >> 
  11. >> X () : X(34) {}             // Not allowed
  12.  
  13. > Well, one could use a similar thing using a member function:
  14.  
  15. > X (int i) { init(i); }
  16. > X () { init(34); }
  17.  
  18. > where init is a member function for class X.
  19.  
  20. The problem is that member functions can't initialize anything.  Often
  21. this is just a minor nuisance: the X ctor calls the default ctor of
  22. the data member, which gets then immediately overwritten by assignment
  23. in init().  But you can't use this technique when you deal with const
  24. and reference members.  Thus the syntax "X () : X(34) {}" would still
  25. be useful.  Has anyone written a proposal?
  26. [ To submit articles: Try just posting with your newsreader.
  27.               If that fails, use mailto:std-c++@ncar.ucar.edu
  28.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  29.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  30.   Comments? mailto:std-c++-request@ncar.ucar.edu
  31. ]
  32.